<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/5
 * Time: 15:16
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use \app\api\service\Token;
use \app\api\service\Message as MessageService;
use app\api\validate\MessageFormIdParameter;
use app\lib\exception\SuccessMessage;
use app\lib\exception\UserException;

class Message extends BaseController
{
    /**
     * 创建formid
     * @param $formId
     * @param $wu_id
     * @return SuccessMessage
     * @throws UserException
     */
    public function createFormId($formId,$w_id){
        (new MessageFormIdParameter())->goCheck();
        $uid = Token::getCurrentUid();
        $t_id = Token::getCurrentTokenVar('manager_id');
        $service = new MessageService();
        $result =  $service->createFormId($formId,$t_id,$w_id,$uid);
        if (!$result){
            throw new UserException([
                'code'=>400,
                'msg'=>'formid添加失败',
                'error_code'=>90000
            ]);
        }
        return json_encode(new SuccessMessage());
    }

    /**
     * 获取用户缓存聊天信息信息
     * @return string
     * @throws UserException
     */
    public function getCacheMessageByUser($wu_id){
        $uid = Token::getCurrentUid();
        $service = new MessageService();
        $result =  $service->getCacheMessage($wu_id,$uid);
        if (!$result){
            throw new UserException([
                'code'=>201,
                'msg'=>'获取缓存信息失败',
                'error_code'=>90000
            ]);
        }
        return json_encode($result);
    }

    /**
     * 用户请求聊天记录
     * @param $wu_id
     * @param $page
     * @return string
     */
    public function getMessageListByUser($w_id,$page){
        $uid = Token::getCurrentUid();
        $service = new MessageService();
        $result = $service->getMessageList($uid,$w_id,$page);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2002;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 改变小程序用户聊天信息已读状态
     * @return string
     * @throws UserException
     */
    public function changeMessageStatusByUser($wu_id){
        $uid = Token::getCurrentUid();
        $type='user';
        $service = new MessageService();
        $result =  $service->changeStatus($wu_id,$uid,$type);
        if (!$result){
            throw new UserException([
                'code'=>201,
                'msg'=>'获取缓存信息失败',
                'error_code'=>90000
            ]);
        }
        return json_encode($result);
    }

    /**
     * 改变CRM用户聊天信息已读状态
     * @param $uid
     * @return string
     * @throws UserException
     */
    public function changeMessageStatusByWorkUser($uid){
        $wu_id = Token::getCurrentUid();
        $type='workuser';
        $service = new MessageService();
        $result =  $service->changeStatus($wu_id,$uid,$type);
        if ($result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2003;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 获取聊天用户
     * @return false|string
     * @throws UserException
     */
    public function getUser(){
        $wu_id = Token::getCurrentUid();
        $service = new MessageService();
        $result =  $service->getCord($wu_id);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2003;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 教练获取聊天记录
     * @param $uid
     * @param $page
     * @return string
     */
    public function getMessageList($uid,$page){
        $wu_id = Token::getCurrentUid();
        $service = new MessageService();
        $result = $service->getMessageList($uid,$wu_id,$page);
        if (!empty($result)){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2004;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 改变CRM用户聊天记录状态
     * @param $uid
     * @return string
     */
    public function changeStatus($uid){
        $wu_id = Token::getCurrentUid();
        $type = 'wokeuser';
        $service = new MessageService();
        $result =  $service->changeStatus($wu_id,$uid,$type);
        if ($result){
            $data['code'] = 200;
            $data['content'] = $result;
        }else{
            $data['code'] = 2005;
            $data['content'] = '';
        }
        return json_encode($data);
    }

    /**
     * 删除联系人
     * @param $uid
     * @return string
     */
    public function deleteUser($uid){
        $wu_id = Token::getCurrentUid();
        $service = new  MessageService();
        $result = $service->deleteUser($uid,$wu_id);
        if ($result){
            $data['code'] = 200;
            $data['content'] = 'success';
        }else{
            $data['code'] = 2006;
            $data['content'] = 'fail';
        }
        return json_encode($data);
    }

}